package com.sosee.cgfw.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.FragmentActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSONArray;
import com.sosee.cgfw.R;
import com.sosee.cgfw.camera.CameraActivity;
import com.sosee.cgfw.entity.JsonData;
import com.sosee.cgfw.entity.User;
import com.sosee.cgfw.entity.ad.ADJson;
import com.sosee.cgfw.friends.FashionFriends;
import com.sosee.cgfw.friends.FriendsListData;
import com.sosee.cgfw.service.ExampleService;
import com.sosee.cgfw.utils.Constans;
import com.sosee.cgfw.utils.LogUtils;
import com.sosee.cgfw.utils.Utils;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import scrollimage.common.ScrollImageEntity;
import scrollimage.image.ImageScroll;
import scrollimage.image.ScrollImageController;

public class MainActivity extends FragmentActivity {

    ImageScroll myPager; // 图片容器
    LinearLayout ovalLayout; // 圆点容器
    private ScrollImageController scrollImageView;
    private List<ScrollImageEntity> list;
    private HttpUtils httpUtils;
    private List<FriendsListData.Data> data;
    private  String realname ;
    private String company;
    private String duty;
    private String nickname;


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        httpUtils=new HttpUtils();
        initScrollView();
        initData();
        getAllDate();
    }

    @Override
    protected void onStart() {
        super.onStart();
        LogUtils.log_E("测试onStart");
        getAD();
    }

    public void getAD(){
        httpUtils.send(HttpRequest.HttpMethod.GET, Constans.AD_URL, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Constans.aList = JSONArray.parseArray(responseInfo.result, ADJson.class);
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Utils.showToast(getString(R.string.internet_error));
            }
        });
    }





    //获取所有活动日程 的日期
    public void getAllDate(){

        httpUtils.send(HttpRequest.HttpMethod.GET, Constans.URL_SCHEDULE, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                LogUtils.log_E("获取活动日程：" + responseInfo.result);
                List<JsonData> list = null;
                list = Utils.getSchedule(responseInfo.result);
                for (int i = 0; i < list.size(); i++) {
                    if (!Utils.list_date.contains(list.get(i).getData().getStart_date())) {
                        Utils.list_date.add(list.get(i).getData().getStart_date());
                        LogUtils.log_E(list.get(i).getData().getStart_date());
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }
    //主界面 点击事件
    public void onClick(View view){
        int id=view.getId();
        switch (id){
            //epass
            case R.id.rl_epass:
                if(Constans.userID!=null){
                    startActivity(new Intent(this,MyTicketActivity.class));
                }else{
                    goLogin(Constans.EPASS);
                }
                break;
            //自拍
            case R.id.rl_zipai:
                startActivity(new Intent(this,CameraActivity.class));
                break;
            //潮流资讯
            case R.id.rl_zixun:
                //测试 捕获全局异常 重启app功能
//                int a=1/0;
                startActivity(new Intent(this, FashionNewsActivity.class));
                break;
            //活动日程
            case R.id.rl_richeng:
                goSedule();
                break;
            //时尚好友
            case R.id.rl_haoyou:
                if(Constans.userID!=null){
                    re(view);
                }else{
                    goLogin(Constans.FREND);
                }
                break;
            //智能助理
            case R.id.rl_zhuli:
                if (Constans.userID!=null){
                    startActivity(new Intent(this, AssistantActivity.class));
                }else{
                    goLogin(Constans.ASSISTANT);
                }
                break;
        }
    }

    private void re(final View view) {
        httpUtils.configCurrentHttpCacheExpiry(100);
        httpUtils.send(HttpRequest.HttpMethod.GET, Constans.USER_INFO_URL + Constans.userID, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                Gson gson= new Gson();
                User user = gson.fromJson(responseInfo.result, User.class);
                nickname=user.getNickname();
                realname = user.getRealname();
                company = user.getCompany();
                duty = user.getDuty();
                System.out.println("111"+realname+"22"+company+"33"+duty);


                if(realname.length()>1&&company.length()>1&&duty.length()>1&&nickname.length()>1){
                startActivity(new Intent(MainActivity.this, FashionFriends.class));
                    }else{
                        //弹框去补全信息。
                        showPopup(view);
                    }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Utils.showToast("网络异常"+s);

            }
        });


    }


    public void goLogin(int flag){
        Intent intent=new Intent(this, LoginActivity.class);
        intent.putExtra(Constans.flag,flag);
        startActivity(intent);
    }

    PopupWindow popupWindow=null;
    public void showPopup(View view){
        popupWindow=new PopupWindow(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        View v=View.inflate(this,R.layout.pop_openinfo_friends,null);
        v.findViewById(R.id.tv_cancle_s).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closePopWindow();
            }
        });
        v.findViewById(R.id.tv_confim_s).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //这里打开信息界面，让其填写。
                startActivity(new Intent(MainActivity.this,PersonalInfo.class));
                closePopWindow();
            }
        });
        popupWindow.setContentView(v);
        popupWindow.showAtLocation(view, Gravity.CENTER,0,0);
    }

    //关闭 pop
    public void closePopWindow(){
        if (popupWindow!=null){
            popupWindow.dismiss();
        }
    }
    //跳转到活动日程界面
    public void goSedule(){
        //处理 异常
        if(Utils.list_date!=null&&Utils.list_date.size()>0){
            startActivity(new Intent(this,ScheduleActivity.class));
        }else{
            Utils.showToast(getString(R.string.internet_error));
            getAllDate();
            if(Utils.list_date!=null&&Utils.list_date.size()>0)
                startActivity(new Intent(this,ScheduleActivity.class));
        }
    }


    public void showMenu(View view) {
        startActivity(new Intent(this, PersonalSettingActivity.class));
    }


    /**
     * 初始化滚动图片布局
     */
    private void initScrollView() {
        myPager = (ImageScroll) findViewById(R.id.myvp);
        ovalLayout = (LinearLayout) findViewById(R.id.vb);
    }


    /**
     * 初始化滚动图片数据数据
     */
    private void initData() {
        scrollImageView = new ScrollImageController(this);
        list = new ArrayList<ScrollImageEntity>();

        if(false){
            //测试数据
            ScrollImageEntity scrollImageEntity1 = new ScrollImageEntity();
            scrollImageEntity1.setTitle("");
            scrollImageEntity1.setImageUrl("http://192.168.10.248:9801/data/advs/5/logo.jpg");
            ScrollImageEntity scrollImageEntity2 = new ScrollImageEntity();

            scrollImageEntity2.setTitle("");
            scrollImageEntity2.setUrl("");
            scrollImageEntity2.setImageUrl("http://192.168.10.248:9801/data/advs/4/logo.jpg");
            ScrollImageEntity scrollImageEntity3 = new ScrollImageEntity();
            scrollImageEntity3.setTitle("");
            scrollImageEntity3.setImageUrl("");
            scrollImageEntity3.setImageUrl("http://192.168.10.248:9801/data/advs/6/logo.jpg");
            list.add(scrollImageEntity1);
            list.add(scrollImageEntity2);
            list.add(scrollImageEntity3);
        }else{
            for (int i=0;i< Constans.aList.size();i++){
                ADJson adJson=Constans.aList.get(i);
                ScrollImageEntity entity=new ScrollImageEntity();
                entity.setImageUrl(Constans.BASE_URL+adJson.getData().getPic());
                entity.setTitle("");
                entity.setUrl("");
                list.add(entity);
                LogUtils.log_E("测试 url"+Constans.BASE_URL+adJson.getData().getPic());
            }
        }

        //启用图片滚动
        scrollImageView.initViewPagerImage(list, myPager, ovalLayout,true);
    }


    private long currTime;
    private long endTime;


    private String url;

    public void getDhub(String duh){
        endTime= System.currentTimeMillis();
        LogUtils.log_E("事件"+endTime+"==="+currTime);
        if((endTime-currTime)>2000){
            LogUtils.log_E("get dhub");
            httpUtils.send(HttpRequest.HttpMethod.GET, duh, new RequestCallBack<String>() {
                @Override
                public void onSuccess(ResponseInfo<String> responseInfo) {
                    try {
                        LogUtils.log_E("duhub json"+responseInfo.result);
                        JSONObject json=new JSONObject(responseInfo.result);
                        url=json.getString("link");
                        Intent intent=new Intent(MainActivity.this,WebActivity.class);
                        intent.putExtra("url","http://"+url);
                        startActivity(intent);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(HttpException e, String s) {
                    Utils.showToast("网络异常");
                }
            });
        }
        currTime=SystemClock.currentThreadTimeMillis();



    }


}
